/**
 * 活动数据分析 - 邀请数据分析 - 邀请教师统计
 *
 * created by qiuwei
 * 2016年05月11日
 */

'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

/**
 * @public
 * 准备请求参数
 *
 * @param {object} requestData 请求数据
 * @return {object} 请求参数
 */
function request(requestData) {
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;

	// 注册人数
	let registered = {
		"data": {
			"target": "teacher-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": filters
	};

	// 被邀请人数
	let beInvited = {
		"data": {
			"target": "teacher-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "teacher-invite_type", //邀请注册类型。0：主动注册；1：通过手机号邀请；2：通过验证码邀请
			"type": ">",
			"value": 0
		}].concat(filters)
	};

	// 被邀请核心人数
	let beInvitedCore = {
		"data": {
			"target": "teacher-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "teacher-invite_type", //邀请注册类型。0：主动注册；1：通过手机号邀请；2：通过验证码邀请
			"type": ">",
			"value": 0
		}, {
			"item": "teacher-student_count",
			"type": ">=",
			"value": 20
		}].concat(filters)
	};

	return [registered, beInvited, beInvitedCore];
}

/**
 * @public
 * 解析响应数据
 *
 * @return {array} 图表数据
 */
function response(registered, beInvited, beInvitedCore) {

	let categories = new Set();
	let toMapHandler = (function(map, item) {
		this.add(item.time_sheet);
		map[item.time_sheet] = item.result;

		return map;
	}).bind(categories);

	let registeredMap = registered.reduce(toMapHandler, {});
	let beInvitedMap = beInvited.reduce(toMapHandler, {});
	let beInvitedCoreMap = beInvitedCore.reduce(toMapHandler, {});

	let rn = {
		categories: Array.from(categories),
		registeredSeries: [],
		beInvitedSeries: [],
		beInvitedCoreSeries: [],
		invitedConversionSeries: [],
		accountingSeries: []
	};

	let registeredItem, beInvitedItem, beInvitedCoreItem;
	rn.categories.forEach((item) => {
		registeredItem = parseInt(registeredMap[item]) || 0;
		rn.registeredSeries.push(registeredItem);

		beInvitedItem = parseInt(beInvitedMap[item]) || 0;
		rn.beInvitedSeries.push(beInvitedItem);

		beInvitedCoreItem = parseInt(beInvitedCoreMap[item]) || 0;
		rn.beInvitedCoreSeries.push(beInvitedCoreItem);

		rn.invitedConversionSeries.push(beInvitedItem ? Math.round(beInvitedCoreItem / beInvitedItem * 100) : 0);
		rn.accountingSeries.push(registeredItem ? Math.round(beInvitedItem / registeredItem * 100) : 0);
	});

	return rn;
}

function filter(condition) {
	let filters = [];

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('teacher-day', '>=', condition.startDate));
	filters.push(builder.append('teacher-day', '<=', condition.endDate));
	// filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}